﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace MyMainSite2.UI.AdminWeb.Helpers
{
	public class FormsAuthenticationHelper : IFormsAuthenticationHelper
	{
		#region Public Methods

		public void Login(ControllerContext controllerContext, string userName)
		{
			// from http://www.codethinked.com/setting-up-authentication-in-aspnet-mvc
			var authTicket = new FormsAuthenticationTicket(1, userName, DateTime.Now,
												   DateTime.Now.AddMinutes(30), true, String.Empty);

			string cookieContents = FormsAuthentication.Encrypt(authTicket);
			var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, cookieContents)
			{
				Expires = authTicket.Expiration,
				Path = FormsAuthentication.FormsCookiePath,
			};

			controllerContext.HttpContext.Response.Cookies.Add(cookie);
		}

		public void Logout()
		{
			FormsAuthentication.SignOut();
		}

		#endregion
	}
}